--|         Kenny2  by  C!chy  -  version 1.0 -- 2006-11-13         |--
--|  Makes users Kenny-clones, and replaces words with Kenny-words  |--
--|  cmds:     !kenny add nick - nick becames a Kenny clone         |--
--|            !kenny remove nick - changes nick back               |--
--|            !kenny list - show Kenny cloned nicks                |--
--|            !kenny help - show available commands                |--
--|  website: http://coyote.ds.tu.koszalin.pl/~cichy/verli/         |--


-- Previous ver.: Kenny2 v0.1.2 by Téper -- 2005-03-13
-- Original version: -- Kenny Script by Hawk -- 31-03-04

--|  set Bot nickname and minimum class to use this command  |--
Bot = "Kenny"
minclass = 4

KennyText = {"*umfl*", "*uuffum*", "*lluu*", "*mlmlff*", "*lfumfl*", "*lmmf*",
  "*uullu*", "*mmmm*", "*ommlu*", "*mflf*", "*olomum*", "*mhhhmmlm*", "*mhhl*",
  "*mujm*"}
         
--| end of config |--



KennylizedNicks = {}

function VH_OnOperatorCommand(Nick, Cmd)
  Cmd, Param = First(Cmd)
  _, class=VH:GetUserClass(Nick)
  if Cmd == "!kenny" then
    SubCmd, Param = First(Param)
    if (SubCmd == "add" and class>=minclass) then
      Kenylize(Param)
      return 0
    elseif (SubCmd == "remove" and class>=minclass) then
      UnKenylize(Param)
      return 0
    elseif (SubCmd == "list" and class>=minclass) then
      ShowKennys(Nick)
      return 0
    elseif (SubCmd == "help" and class>=minclass) then
      KennyHelp(Nick)
      return 0
    end
  end
  return 1
end

function VH_OnParsedMsgChat(Nick, msg)
  if KennylizedNicks[Nick] == 1 then
    text = string.gsub(msg, "%w+",
      function ()
        return KennyText[math.random(1, table.getn(KennyText))]
      end
    )
    VH:SendDataToAll("<"..Nick.."> "..text.."|", 0, 10)
    return 0
  end
  return 1
end
                            

function Kenylize(User)
  if KennylizedNicks[User] == nil then
    KennylizedNicks[User] = 1
    VH:SendDataToAll(CreateChat(Bot, User.." became a Kenny clone..."), 0, 10)
  end
end

function UnKenylize(User)
  if User == "all" then
    KennylizedNicks = {}
    VH:SendDataToAll(CreateChat(Bot, "All Kenny clones changed back..."), 0, 10)
  elseif KennylizedNicks[User] == 1 then
    KennylizedNicks[User] = nil;
    VH:SendDataToAll(CreateChat(Bot, User.." changed back..."), 0, 10)
  end
end

function ShowKennys(User)
  local Names = ""
  for line, val in pairs(KennylizedNicks) do
    Names = Names.." "..line.."\r\n"
  end
  VH:SendDataToUser(CreateChat(Bot, "Kennylized users:\r\n"..Names), User)
end

function KennyHelp(User)
  VH:SendDataToUser(CreateChat(Bot, "Help on Kennylization:\r\n"..
    "- !kenny add <nick>\r\n- !kenny remove <nick> (also ''all'')\r\n"..
    "- !kenny list\r\n- !kenny help"),
    User)
end


function CreateChat(From, Msg)
  return "<"..From.."> "..Msg.."|"
end

function CreaterPM(From, To, Msg)
  return "$To: "..To.." $From: "..From.." $"..CreateChat(From, Msg)
end

function First(Str, Sep)
  if Sep == nil then
    Sep = "%s"
  end
  local i = string.find(Str, Sep)
  if i == nil then
    return Str, ""
  else
    return string.sub(Str, 1, i-1), string.sub(Str, i+1)
  end
end
